package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.producer.MsgProducer;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ResultBuilder;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @author jokerpan
 */
@Controller
@Secured("ROLE_ADMIN")
public class RabbitController {

    @Resource
    private MsgProducer msgProducer;

    /**
     * 发送消息到队列A
     * @return
     */
    @ResponseBody
    @AccessLimit(seconds = 6, maxCount = 5)
    @RequestMapping("/rabbitmq/sendMsg")
    public String sendMsg(){
        int msgNum = 100;
        for(int i=0;i<msgNum;i++) {
            msgProducer.sendMsg("这是发送的第"+i+"条消息");
        }
        return "success";
    }


    /**
     * 发送消息到队列B
     * @return
     */
    @ResponseBody
    @AccessLimit(seconds = 6, maxCount = 5)
    @RequestMapping("/rabbitmq/sendMsgToQueueB")
    public String sendMsgToQueueB(){
        int msgNum = 100;
        for(int i=1;i<=msgNum;i++) {
            msgProducer.sendMsgToQueueB("这是发送的第"+i+"条消息");
        }
        return "success";
    }


    /**
     * 发送消息到队列B
     * @return
     */
    @ResponseBody
    @AccessLimit(seconds = 6, maxCount = 5)
    @RequestMapping("/rabbitmq/sendMsgAll")
    public String sendMsgAll(){
        int msgNum = 10;
        for(int i=1;i<=msgNum;i++) {
            msgProducer.sendAll("这是发送的第"+i+"条消息");
        }
        return "success";
    }

    /**
     * 测试发送验证码
     */
    @ResponseBody
    @AccessLimit(seconds = 6, maxCount = 5)
    @RequestMapping("/rabbitmq/sendCode")
    public ActionResult sendCode(){
        String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb=new StringBuilder(6);
        for(int i=0;i<6;i++) {
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        msgProducer.sendAll("【验证码】："+sb.toString());
        return ResultBuilder.buildActionSuccess();
    }
}
